import { CollectDataProps, MCollect } from "@/components/MCollect/index.vue";
export interface MCollectProps {
  getDataModelDetail: (data: any[], modelStr: string, countStr: string) => void;
  setCheckedData: (multipleSelection: any[], isOpen?: boolean, label?: string, prop?: string, title?: string) => void;
}

export const useCollect = () => {
  const state = reactive<MCollect>({
    visible: false,
    // 标题1
    title1: "",
    title1Data: [],
    title2Data: [],
    loading: false
  });

  /**
   * @param {Array} data 表格数据
   * @param {string} modelStr  表格中型号字段
   * @param {string} countStr 表格中重量字段
   * @description 获取数据中的产品汇总
   */
  const getDataModelDetail = (data: any[], modelStr: string, countStr: string) => {
    if (!data?.length) return [];
    const children: CollectDataProps<number>[] = [];
    let modelNames = [];
    for (let item of data) {
      const index = modelNames.indexOf(item[modelStr]);
      if (index === -1) {
        modelNames.push(item[modelStr]);
        children.push({
          label: item[modelStr],
          value: parseFloat(item[countStr])
        });
      } else {
        children[index].value += parseFloat(item[countStr]);
      }
    }
    return children;
  };

  /**
   * @param {Array}  tableData 返回的表格字段
   * @description 对后端返回的字段汇总字段进行处理
   */
  const getSearchDetail = tableData => {
    const { count, sum, group } = tableData;
    state.title1 = "产品汇总";
    state.title1Data = [
      {
        label: "总重量",
        value: sum
      },
      {
        label: "总包数",
        value: count
      }
    ];
    state.title2Data[0] = {
      title: "产品明细汇总",
      children: []
    };
    state.title2Data[0].children = group.map(son => {
      const { key, value } = son;
      return {
        label: key,
        value
      };
    });
  };

  /**
   * @description: 设置选中的明细数据
   * @param {any} multipleSelection 表格的数据
   * @param {string} label 显示的标签
   * @param {string} prop 绑定的字段
   * @param {boolean} prop 是否直接打开
   * @param {*} title 明细显示标题
   * @return {*}
   */
  const setCheckedData = (
    multipleSelection: any[],
    isOpen: boolean = true,
    label: string = "m_mname",
    prop: string = "netWeight",
    title = "产品明细汇总"
  ): void => {
    state.visible = isOpen;
    state.title2Data = [];
    let children: CollectDataProps[] = [];
    let labels = [];

    let sum = 0;

    // 设置产品明细汇总
    for (let son of multipleSelection) {
      const index = labels.indexOf(son[label]);
      let value = typeof son[prop] === "string" ? parseFloat(son[prop]) : son[prop];
      if (index === -1) {
        labels.push(son[label]);
        children.push({
          label: son[label],
          value,
          prop
        });
      } else {
        children[index].value += value;
      }
      sum += value;
    }
    state.title2Data.push({
      title: title,
      children
    });

    // 设置产品汇总
    state.title1 = "产品汇总";
    state.title1Data = [
      {
        label: "总包数",
        prop: "count",
        value: multipleSelection.length
      },
      {
        label: "总重量",
        prop: "sum",
        value: sum
      }
    ];
  };
  return {
    ...toRefs(state),
    getDataModelDetail,
    setCheckedData,
    getSearchDetail
  };
};
